<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Category;
use App\Newslist;

class NewController extends Controller
{
    public function newsCate(){
        $catelist = Category::paginate(8);
        return view('admin.news_category_list',['catelist'=>$catelist]);
    }

    public function newsList(){
        $newslist = Newslist::paginate(8);
        $catelist = Category::get();
        return view('admin.news_list',['newslist'=>$newslist,'catelist'=>$catelist]);
    }

    //增加分类
    public function addCate(){

        return view('admin.add_news_category');
    }

    public function postCate(){
        $adcate = new Category();
        $adcate->catname = $_POST['catname'];
        $adcate->keywords = $_POST['keywords'];
        $adcate->description = $_POST['description'];
        $adcate->url = $_POST['url'];
        $adcate->isshow = $_POST['isshow'];
        return $adcate->save() ? "OK" : "fail";

    }
    //删除分类
    public function delCate($catid){
        $delcate = Category::find($catid);
        return $delcate->delete() ? "OK" : "fail";

    }
    //编辑分类
    public function editCate($catid){
        if (empty($_POST)) {
            $editcate = Category::find($catid);
            return view('admin.edit_news_category',['editcate'=>$editcate]);
        }else{
            $editcate = Category::find($catid);
            $editcate->catname = $_POST['catname'];
            $editcate->keywords = $_POST['keywords'];
            $editcate->description = $_POST['description'];
            $editcate->url = $_POST['url'];
            $editcate->isshow = $_POST['isshow'];
            return $editcate->save() ? "OK" : "fail";
        }



    }

    //添加新闻
    public function addNews(){
        $catelist = Category::get();
        return view('admin.add_news',['catelist'=>$catelist]);
    }

    public function postNews(Request $req){
        // dd($req->all());
        $dpath = '/admin/img';
        $fname = date('Ymd').mt_rand(1000,9999).'.jpg';
        $req->file('n_pic')->move($dpath,$fname);

        $addnews = new Newslist();
        $addnews->catid = $_POST['catid'];
        $addnews->n_title = $_POST['n_title'];
        $addnews->n_keywords = $_POST['n_keywords'];
        $addnews->n_author = $_POST['n_author'];
        $addnews->n_pic = $dpath.$fname;
        $addnews->isrecom = $_POST['isrecom'];
        $addnews->n_hits = $_POST['n_hits'];
        $addnews->n_info = $_POST['n_info'];
        $addnews->n_addtime = time();
        return $addnews->save() ? "OK" : "fail";

    }
    //删除新闻
    public function delNews($n_id){
        $dellist = Newslist::find($n_id);
        return $dellist->delete() ? "OK" : "fail";
    }

    //编辑新闻
    public function editNews(Request $req,$n_id){
        if (empty($_POST)) {
            $catelist = Category::get();
            $newslist = Newslist::find($n_id);
            return view('admin.edit_news',['newslist'=>$newslist,'catelist'=>$catelist]);
        }else{
            $dpath = '/admin/img';
            $fname = date('Ymd').mt_rand(1000,9999).'.jpg';
            $req->file('n_pic')->move($dpath,$fname);

            $newslist = Newslist::find($n_id);
            $newslist->catid = $_POST['catid'];
            $newslist->n_title = $_POST['n_title'];
            $newslist->n_keywords = $_POST['n_keywords'];
            $newslist->n_author = $_POST['n_author'];
            $newslist->n_pic = $dpath.$fname;
            $newslist->isrecom = $_POST['isrecom'];
            $newslist->n_hits = $_POST['n_hits'];
            $newslist->n_info = $_POST['n_info'];
            $newslist->n_addtime = time();
            return $newslist->save() ? "OK" : "fail";
        }
    }
}
